package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.QuerySemanticEngineDetailReq;
import com.sg.dto.base.component.req.QuerySemanticEngineListReq;
import com.sg.entity.OmsTemplateSemanticEngine;
import com.sg.service.base.component.MOmsTemplateSemanticEngineService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsTemplateSemanticEngineMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTemplateSemanticEngineServiceImpl
        extends ServiceImpl<OmsTemplateSemanticEngineMapper, OmsTemplateSemanticEngine>
        implements MOmsTemplateSemanticEngineService {

    @Autowired
    OmsTemplateSemanticEngineMapper omsTemplateSemanticEngineMapper;

    /**
     * 2-8-1查语义引擎列表[1366]
     * gen by moon at 8/17/2022, 7:58:26 PM
     */
    @Trace(operationName = "2-8-1查语义引擎列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTemplateSemanticEngine> querySemanticEngineList(QuerySemanticEngineListReq reqDto) {

        try {
            QueryWrapper<OmsTemplateSemanticEngine> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTemplateSemanticEngine.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTemplateSemanticEngine.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTemplateSemanticEngine.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsTemplateSemanticEngine>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-8-1修改语义引擎[1364]
     * gen by moon at 8/17/2022, 7:58:25 PM
     */
    @Trace(operationName = "2-8-1修改语义引擎")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSemanticEngine(OmsTemplateSemanticEngine omsTemplateSemanticEngine) {
        // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(omsTemplateSemanticEngine.getPictureSample())){
//         omsTemplateSemanticEngine.setPictureSample(CommonFunctionHelper.replaceFilePath(omsTemplateSemanticEngine.getPictureSample()));
//     }
        omsTemplateSemanticEngine.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTemplateSemanticEngine.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTemplateSemanticEngine.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTemplateSemanticEngine.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTemplateSemanticEngine.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTemplateSemanticEngine.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsTemplateSemanticEngine.setIsValid("FALSE");
        return super.updateById(omsTemplateSemanticEngine);
    }

    /**
     * 2-8-1新增语义引擎[1363]
     * gen by moon at 8/17/2022, 7:58:24 PM
     */
    @Trace(operationName = "2-8-1新增语义引擎")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSemanticEngine(OmsTemplateSemanticEngine omsTemplateSemanticEngine) {
        // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(omsTemplateSemanticEngine.getPictureSample())){
//         omsTemplateSemanticEngine.setPictureSample(CommonFunctionHelper.replaceFilePath(omsTemplateSemanticEngine.getPictureSample()));
//     }
        omsTemplateSemanticEngine.setTemplateSemanticEngineId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTemplateSemanticEngine.setIsArchive("FALSE");
        omsTemplateSemanticEngine.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTemplateSemanticEngine.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTemplateSemanticEngine.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsTemplateSemanticEngine.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTemplateSemanticEngine.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTemplateSemanticEngine != null && omsTemplateSemanticEngine.getSubjectLifeCycle() != null && omsTemplateSemanticEngine.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTemplateSemanticEngine.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsTemplateSemanticEngine.setIsValid("FALSE");
        omsTemplateSemanticEngine.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTemplateSemanticEngine.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTemplateSemanticEngine)) {
            return omsTemplateSemanticEngine.getTemplateSemanticEngineId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-8-1查语义引擎详情[1186]
     * gen by moon at 6/4/2024, 2:43:13 PM
     */
    @Trace(operationName = "2-8-1查语义引擎详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTemplateSemanticEngine querySemanticEngineDetail(QuerySemanticEngineDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsTemplateSemanticEngine> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTemplateSemanticEngine> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTemplateSemanticEngine retData = retList.get(0);
                retData.setPictureSample(CommonFunctionHelper.getFilePath(retData.getPictureSample()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-8-1删除语义引擎[1365]
     * gen by moon at 8/17/2022, 7:58:25 PM
     */
    @Trace(operationName = "2-8-1删除语义引擎")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSemanticEngine(String templateSemanticEngineId) {

        return super.removeById(templateSemanticEngineId);
    }
}
